package controllers

import (
	"beego_blog/services"
)

type UserController struct {
	baseController
	service services.UserService
}

func (c *UserController) Add() {
	if c.Ctx.Request.Method == "POST" {
		c.service.Add(c.Input())
		c.SuccessJson(true)
	}
}

func (c *UserController) Delete() {
	if c.Ctx.Request.Method == "POST" {
		c.service.Delete(c.Input())
		c.SuccessJson(true)
	}
}

func (c *UserController) Query() {
	if c.Ctx.Request.Method == "POST" {
		c.SuccessJson(c.service.Query(c.Input()))
	}
}

func (c *UserController) Update() {
	if c.Ctx.Request.Method == "POST" {
		c.service.Update(c.Input())
		c.SuccessJson(true)
	}

}

func (c *UserController) Login() {
	if c.Ctx.Request.Method == "POST" {
		ok, auth_s := c.service.Login(c.Input())
		if ok {
			c.Ctx.Output.Cookie("username", c.Input().Get("username"))
			c.Ctx.Output.Cookie("Authorization", auth_s)
		}

		c.SuccessJson(ok)
	}

}

func (c *UserController) Prepare() {
	c.service = services.UserService{}
	c.service.Prepare()
}
